<?php

/**
 * Класс RL_RequestRouter_Community -- сопоставляет строку с view community
 */
class RL_RequestRouter_Community extends RL_RequestRouter_Regexp
{

	public function __construct(RL_RequestRouter $router, MysqlStore $store, Array $params, array $action_map=array())
	{
		parent::__construct($router, $params);
		$this->action_map = $action_map;
		$this->store = $store;
	}

	/**
	 * Сопоставить $url с правилами и вернуть (дерево разбора?)
	 *
	 * $o = new RL_RequestRouter_Url(array(
	 *       "(:user)/and/(:user)",              'user'      => '[a-zA-Z]+',
	 *                                           '_action'   => 'Users.me',
	 *    )),
	 * $matches = $o->match('hannah/and/hannah');
	 * @param string $url
	 * @return array or NULL
	 */
	public function match($url)
	{
		$request = parent::match($url);
		if ($request) {
			// find community by $request['community_url']
			$store = $this->store;
			$community = $store->find('select objects where community.community_url = :0', array($request['community_url']))->next();
			if (empty($community)) {
				$request = NULL;
			} else {
				$tdi = $store->find('select objects where tdi.url = :0', array($request['tdi_url']))->next();
				if (empty($tdi)) {
					$request = NULL;
				} else {
					if (isset($tdi->data['view']) && array_key_exists($tdi->data['view'], $this->action_map)) {
						$request['path'] = $tdi->data['view'];
						$request['action'] = $this->action_map[$tdi->data['view']];
					} else {
						//$request['action'] = 'showfeed';
						$request = NULL;
					}
				}
			}
			// find tdi by $request['tdi_url'] if exists
		}
		return $request;
	}

}


